package leetcode.editor.week.week315;

// 6219. 反转之后的数字和
// https://leetcode.cn/problems/sum-of-number-and-its-reverse/
public class Solution6219 {
    public boolean sumOfNumberAndReverse(int num) {
        for (int i = 0; i <= num; i++) {
            if (i + reverse(i) == num) return true;
        }

        return false;
    }

    public int reverse(int n) {
        String s = rev(String.valueOf(n));
        return Integer.parseInt(s);
    }

    public String rev(String s) {
        StringBuilder sb = new StringBuilder(s);
        int n = s.length();
        int i = 0, j = n - 1;
        while (i <= j) {
            char c = sb.charAt(i);
            sb.setCharAt(i, sb.charAt(j));
            sb.setCharAt(j, c);
            i++;
            j--;
        }

        return sb.toString();
    }
}
